home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / prcgntn1.lha / Precognition / source / ScrollingList.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  10KB  |  388 lines

  1. #include <string.h>
  2. #include "minmax.h"
  3. #include "ScrollingList.h"
  4. #include "ScrollingListClass.h"
  5. #include "EmbossedGadgetClass.h"
  6. #include "precognition.h"
  7. #include <proto/exec.h>
  8. #include <proto/intuition.h>
  9. #include <proto/graphics.h>
  10. #include "amigamem.h"
  11.  
  12. #define CHAR_WIDTH  8   /* Width  of Topaz 80 font. */
  13. #define CHAR_HEIGHT 9   /* Height of Topaz 80 font. */
  14. #define BROWSER_ENTRY_HEIGHT (CHAR_HEIGHT + 1)
  15. #define BORDER_WIDTH 3
  16. #define BASELINE    7
  17.  
  18.  
  19. void ScrollingList_SetKnobInfo( ScrollingList *self )
  20. {
  21.    USHORT position, knobsize, nElts, nRows;
  22.  
  23.  
  24.    nElts = self->list.List.nEntries;
  25.  
  26.    if (nElts == 0)
  27.    {
  28.       position = 0;
  29.       knobsize = 0xFFFF;
  30.    }
  31.    else
  32.    {
  33.       nRows = self->list.nRows;
  34.  
  35.       position  = ((ULONG) self->list.YOffset << 16) / nElts;
  36.  
  37.       if (nElts <= nRows)
  38.       {
  39.          knobsize = 0xFFFF;
  40.       }
  41.       else
  42.       {
  43.          knobsize = ((ULONG) nRows << 16) / nElts;
  44.       }
  45.    }
  46.    SetKnobSize( &self->vscroller, knobsize );
  47.    SetValue( &self->vscroller, position );
  48. }
  49.  
  50.  
  51. void ScrollingList_PositionList( ScrollingList *self )
  52. {
  53.    USHORT position,     /* Position of self->vscroller.   */
  54.           new_YOffset,  /* new list YOffset.              */
  55.           nElts,        /* # of elements in list.         */
  56.           nRows,        /* # of rows in display           */
  57.           range;        /* range of positions for YOffset */
  58.  
  59.    ULONG temp;
  60.  
  61.    nElts    = self->list.List.nEntries;
  62.  
  63.    if (nElts)
  64.    {
  65.       nRows    = self->list.nRows;
  66.  
  67.       if ((range = nElts - nRows +1) > 0)
  68.       {
  69.          position = Value( &self->vscroller );
  70.  
  71.          temp = position * range;
  72.          new_YOffset = temp >>16;
  73.          new_YOffset = MIN( new_YOffset, nElts-1);
  74.  
  75.          if (new_YOffset != self->list.YOffset)
  76.          {
  77.             self->list.YOffset = new_YOffset;
  78.             Refresh( &self->list );
  79.          }
  80.       }
  81.    }
  82. }
  83.  
  84.  
  85. void ScrollingList_Init(  ScrollingList *self,
  86.                           PIXELS         LeftEdge,
  87.                           PIXELS         TopEdge,
  88.                           PIXELS         Width,
  89.                           PIXELS         Height,
  90.                           pcg_3DPens     Pens,
  91.                           BOOL           SelectMany )
  92. {
  93.    Gadget *g;
  94.  
  95.    StringLister_Init( self );
  96.    self->slister.isa  = ScrollingListClass();
  97.    self->Pens         = Pens;
  98.  
  99.    ListBrowser_Init( &self->list, 0,0, 0,0, Pens, SelectMany );
  100.  
  101.    VScroller_Init( &self->vscroller, 0,0, Height, Pens, NULL );
  102.  
  103.    for ( g = FirstGadget( &self->list );
  104.          g->NextGadget != NULL;
  105.          g = g->NextGadget );
  106.  
  107.    g->NextGadget = FirstGadget( &self->vscroller );
  108.    self->list.eg.Next = &self->vscroller;
  109.  
  110.    SetSize( self, Width, Height );
  111.    SetLocation( self, LeftEdge, TopEdge );
  112. }
  113.  
  114. void ScrollingList_CleanUp( ScrollingList *self )
  115. {
  116.    CleanUp( &self->list );
  117.    CleanUp( &self->vscroller );
  118. }
  119.  
  120. USHORT ScrollingList_nGadgets( ScrollingList *self )
  121. {
  122.    return nGadgets( &self->list ) + nGadgets( &self->vscroller );
  123. }
  124.  
  125. Gadget *ScrollingList_FirstGadget( ScrollingList *self )
  126. {
  127.    return FirstGadget( &self->list );
  128. }
  129.  
  130. Point ScrollingList_AskSize( ScrollingList *self,
  131.                              PIXELS         Width,
  132.                              PIXELS         Height )
  133. {
  134.    Point size, list_size, vscroller_size;
  135.  
  136.    vscroller_size = AskSize( &self->vscroller, Width, Height );
  137.    /* vscroller's have a fixed width */
  138.  
  139.    list_size.x = Width - vscroller_size.x;
  140.    list_size.y = vscroller_size.y;
  141.  
  142.    list_size = AskSize( &self->list, list_size.x, list_size.y );
  143.  
  144.    size.x = list_size.x + vscroller_size.x;
  145.    size.y = list_size.y;
  146.  
  147.    return size;
  148. }
  149.  
  150.  
  151. Point ScrollingList_SetSize( ScrollingList *self,
  152.                               PIXELS       Width,
  153.                               PIXELS       Height )
  154. {
  155.    Point size, list_size, vscroller_size;
  156.  
  157.    size = AskSize( self, Width, Height );
  158.  
  159.    vscroller_size = SetSize( &self->vscroller, size.x, size.y );
  160.    list_size  = SetSize( &self->list, size.x-vscroller_size.x, size.y );
  161.    self->slister.Size = size;
  162.  
  163.    SetLocation( self, self->slister.Location.x, self->slister.Location.y );
  164.    ScrollingList_SetKnobInfo( self );
  165.  
  166.    return size;
  167. }
  168.  
  169. Point ScrollingList_SetLocation( ScrollingList *self,
  170.                                   PIXELS         LeftEdge,
  171.                                   PIXELS         TopEdge )
  172. {
  173.    Point size, location;
  174.  
  175.    size = Size( &self->list );
  176.    SetLocation( &self->list, LeftEdge, TopEdge );
  177.    SetLocation( &self->vscroller, LeftEdge+size.x, TopEdge );
  178.  
  179.    location.x             = LeftEdge;
  180.    location.y             = TopEdge;
  181.    self->slister.Location = location;
  182.  
  183.    return location;
  184. }
  185.  
  186.  
  187. USHORT ScrollingList_SetYOffset( ScrollingList *self,
  188.                                  USHORT         YOffset )
  189. {
  190.    return ListBrowser_SetYOffset( &self->list, YOffset );
  191. }
  192.  
  193.  
  194. void ScrollingList_Render( ScrollingList  *self,
  195.                            RastPort       *RPort )
  196. {
  197.    Render( &self->list,       RPort );
  198.    Render( &self->vscroller,  RPort );
  199. }
  200.  
  201.  
  202. void ScrollingList_SelectAll( ScrollingList *self, BOOL Select )
  203. /* Selects or deselects everything in the list. */
  204. {
  205.    ListBrowser_SelectAll( &self->list, Select );
  206. }
  207.  
  208.  
  209. const StringList *ScrollingList_StringList_of( ScrollingList *self )
  210. {
  211.    return StringList_of( &self->list );
  212. }
  213.  
  214. void ScrollingList_AddString( ScrollingList  *self,
  215.                               char           *entry,
  216.                               UBYTE           qualifier )
  217. {
  218.    AddString( &self->list, entry, qualifier );
  219.    ScrollingList_SetKnobInfo( self );
  220. }
  221.  
  222.  
  223. void ScrollingList_DeleteString( ScrollingList  *self,
  224.                                  USHORT          entry )
  225. {
  226.    DeleteString( &self->list, entry );
  227.    ScrollingList_SetKnobInfo( self );
  228. }
  229.  
  230. void ScrollingList_DeleteAllStrings( ScrollingList  *self )
  231. {
  232.    DeleteAllStrings( &self->list );
  233.    ScrollingList_SetKnobInfo( self );
  234. }
  235.  
  236. void ScrollingList_SelectString( ScrollingList  *self,
  237.                                  USHORT          entry,
  238.                                  BOOL            Select   )
  239. {
  240.    SelectString( &self->list, entry, Select );
  241. }
  242.  
  243.  
  244. void ScrollingList_Refresh( ScrollingList *self )
  245. {
  246.    Refresh( &self->list      );
  247.    Refresh( &self->vscroller );
  248. }
  249.  
  250. USHORT ScrollingList_ClaimEvent(  ScrollingList  *self,
  251.                                  IntuiMessage   *event )
  252. {
  253.    return ClaimEvent( &self->list,event )
  254.         | ClaimEvent( &self->vscroller, event );
  255. }
  256.  
  257.  
  258. #define CLAIM_EVENT(b,e) (&b->g == (Gadget *) e->IAddress)
  259.  
  260. USHORT ScrollingList_Respond( ScrollingList  *self,
  261.                               IntuiMessage   *Event )
  262. {
  263.    USHORT        list_response;
  264.    USHORT        vscroller_response = 0;
  265.  
  266.    list_response = Respond( &self->list, Event );
  267.  
  268.    if (! (list_response & CONSUMED_EVENT))
  269.    {
  270.       vscroller_response = Respond( &self->vscroller, Event );
  271.       if (vscroller_response & CHANGED_STATE)
  272.       {
  273.          ScrollingList_PositionList( self );
  274.          /*  scroll list */
  275.          vscroller_response = RESPONDED; /* Don't propagate the CHANGED_STATE. */
  276.       }
  277.    }
  278.    return list_response | vscroller_response;
  279. }
  280.  
  281.  
  282. void ScrollingList_SetInteractorWindow( ScrollingList *self,
  283.                                         pcgWindow     *window )
  284. {
  285.    self->slister.IaWindow = window;
  286.    SetInteractorWindow( &self->list, window );
  287.    SetInteractorWindow( &self->vscroller, window );
  288. }
  289.  
  290. ULONG ScrollingList_IDCMPFlags( ScrollingList *self )
  291. {
  292.    return IDCMPFlags( &self->list ) | IDCMPFlags( &self->vscroller );
  293. }
  294.  
  295. BOOL ScrollingList_EnableIactor( ScrollingList *self, BOOL enable )
  296. {
  297.    EnableIactor( &self->list,      enable );
  298.    EnableIactor( &self->vscroller, enable );
  299.    return enable;
  300. }
  301.  
  302. #ifdef BUILDER
  303. #include "BuilderMethods.h"
  304. #include "GraphicObject_Builder.h"
  305. #include "ScrollingList_Builder.h"
  306. #include "ScrollingList_Coder.h"
  307.  
  308. void ScrollingList_PropEdit( ScrollingList *self,
  309.                              pcgWindow     *window,
  310.                              pcg_3DPens     pens );
  311.  
  312. ScrollingList *ScrollingList_New( ScrollingList *self )
  313. {
  314.    ScrollingList *new_list = NULL;
  315.    Point loc, size;
  316.  
  317.    if (new_list = (ScrollingList*) Amalloc(sizeof(ScrollingList)))
  318.    {
  319.       loc  = Location(self);
  320.       size = Size(self);
  321.  
  322.       ScrollingList_Init( new_list, loc.x, loc.y,
  323.          size.x, size.y, self->Pens, self->list.SelectMany );
  324.       GiveItAName( new_list );
  325.  
  326.       StringList_Dup( &self->list.List, &new_list->list.List );
  327.  
  328.    }
  329.    return new_list;
  330. }
  331.  
  332. struct BuilderMethods ScrollingList_bm;
  333.  
  334. #endif
  335.  
  336. BOOL ScrollingList_elaborated = FALSE;
  337.  
  338. struct StringListerClass ScrollingList_Class;
  339.  
  340. void ScrollingListClass_Init( struct StringListerClass *class )
  341. {
  342.    StringListerClass_Init( class );
  343.    class->isa         = StringListerClass();
  344.    class->CleanUp     = ScrollingList_CleanUp;
  345.    class->ClassName   = "ScrollingList";
  346.    class->AskSize     = ScrollingList_AskSize;
  347.    class->SetSize     = ScrollingList_SetSize;
  348.    class->SetLocation = ScrollingList_SetLocation;
  349.    class->Respond     = ScrollingList_Respond;
  350.    class->Refresh     = ScrollingList_Refresh;
  351.    class->Render      = ScrollingList_Render;
  352.    class->ClaimEvent  = ScrollingList_ClaimEvent;
  353.    class->nGadgets    = ScrollingList_nGadgets;
  354.    class->FirstGadget = ScrollingList_FirstGadget;
  355.    class->SetInteractorWindow = ScrollingList_SetInteractorWindow;
  356.    class->IDCMPFlags  = ScrollingList_IDCMPFlags;
  357.    class->isEnabled   = EmbossedGadget_isEnabled;
  358.    class->EnableIactor      = ScrollingList_EnableIactor;
  359.  
  360.    class->StringList_of = ScrollingList_StringList_of;
  361.    class->AddString     = ScrollingList_AddString;
  362.    class->DeleteString  = ScrollingList_DeleteString;
  363.    class->DeleteAllStrings = ScrollingList_DeleteAllStrings;
  364.    class->SelectString  = ScrollingList_SelectString;
  365.  
  366. #ifdef BUILDER
  367.    go_InitBuilderMethods( &ScrollingList_bm );
  368.  
  369.    ScrollingList_bm.New       = ScrollingList_New;
  370.    ScrollingList_bm.PropEdit  = ScrollingList_PropEdit;
  371.    ScrollingList_bm.WriteCode = ScrollingList_WriteCode;
  372.    class->BuilderMethods      = &ScrollingList_bm;
  373. #endif
  374. }
  375.  
  376.  
  377.  
  378. struct StringListerClass *ScrollingListClass( void )
  379. {
  380.    if (! ScrollingList_elaborated)
  381.    {
  382.       ScrollingListClass_Init( &ScrollingList_Class );
  383.       ScrollingList_elaborated = TRUE;
  384.    }
  385.  
  386.    return &ScrollingList_Class;
  387. }
  388.